package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityChangjingDetail;
import nero.yncc.com.createart.bean.BeanGoodChangjing;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.utils.DialogUtil;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.SomeUtil;
import nero.yncc.com.createart.utils.TextUtil;
import nero.yncc.com.createart.utils.ToastUtil;

/**
 * Created by nero on 17-6-27.
 */

public class AdapteChangjingDetailList extends RecyclerView.Adapter<AdapteChangjingDetailList.Holder> {
    public static int viewHeight = 0;
    Context context;
    ArrayList<BeanGoodChangjing> arrayList;
    HashMap<String, BeanGoodChangjing> hashMap;
    ActivityChangjingDetail activityChangjingDetail;

    public AdapteChangjingDetailList(Context context, ArrayList<BeanGoodChangjing> arrayList, HashMap<String, BeanGoodChangjing> hashMap) {
        this.context = context;
        this.arrayList = arrayList;
        this.hashMap = hashMap;
        activityChangjingDetail = (ActivityChangjingDetail) context;
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_detail_list, null, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {

        final BeanGoodChangjing beanGoodChangjing = arrayList.get(position);

        final ArrayList<GodBean.SpecAttr> arrayList = beanGoodChangjing.getGoods().getGoods_spec_list();
//
        holder.tv_money_del.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        holder.tv_money_del.setText(beanGoodChangjing.getGoods().getMarket_price());
        holder.tv_money.setText(beanGoodChangjing.getGoods().getShop_price());
        holder.tv_name.setText(beanGoodChangjing.getGoods().getGoods_name());
        holder.tv_num.setText(beanGoodChangjing.getNum() + "");
        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                beanGoodChangjing.setNum(beanGoodChangjing.getNum() + 1);
                activityChangjingDetail.setGoodsPriceAndNum();
                AdapteChangjingDetailList.this.notifyDataSetChanged();
            }
        });
        holder.tv_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (beanGoodChangjing.getNum() > 0) {
                    beanGoodChangjing.setNum(beanGoodChangjing.getNum() - 1);
                    activityChangjingDetail.setGoodsPriceAndNum();
                    AdapteChangjingDetailList.this.notifyDataSetChanged();
                }
            }
        });

        holder.tv_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText editText = new EditText(context);
                editText.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
                editText.setBackgroundResource(R.drawable.edit_selector_background);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
                params.setMargins(10, 10, 10, 10);
                editText.setLayoutParams(params);
                DialogUtil.getDialog(context).setView(editText).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        holder.tv_num.setText(editText.getText().toString());
                    }
                });
            }
        });
        ImageUtils.BindImage(context, holder.iv_image, beanGoodChangjing.getGoods().getOriginal_img(), 0);

        holder.rc_list.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        AdapterDetailIn adapterDetailIn = new AdapterDetailIn(context, arrayList, hashMap);
        holder.rc_list.setAdapter(adapterDetailIn);
//        SomeUtil.setListViewHeightBasedOnChildren(holder.rc_list);
        adapterDetailIn.notifyDataSetChanged();
//        if (viewHeight == 0) {
//            view.measure(0, 0);
//            viewHeight = view.getMeasuredHeight();
//        }
        if (beanGoodChangjing.isChoose()) {
            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
        } else {
            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.textColorGray));
        }
        holder.icon_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (beanGoodChangjing.isChoose()) {
                    beanGoodChangjing.setChoose(false);
                    hashMap.get(beanGoodChangjing.getGoods().getGoods_id()).setChoose(false);
                } else {
                    //判断规格是否选取
                    ArrayList<GodBean.SpecAttr> arrayList_spec_cache=beanGoodChangjing.getGoods().getGoods_spec_list();
                    for (GodBean.SpecAttr specAttr:arrayList_spec_cache){
                        if(TextUtils.isEmpty(specAttr.getChooseAttrId())){
                            ToastUtil.showToast(context,"请先选择"+specAttr.getName());
                            return;
                        }
                    }
                    beanGoodChangjing.setChoose(true);
                    hashMap.get(beanGoodChangjing.getGoods().getGoods_id()).setChoose(true);
                }
                activityChangjingDetail.setGoodsPriceAndNum();
                AdapteChangjingDetailList.this.notifyDataSetChanged();
            }
        });

    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        MyRecyclerView rc_list;
        TextView tv_money_del, tv_money, tv_name, tv_add, tv_cut, tv_num;
        IconView icon_choose;
        ImageView iv_image;

        public Holder(View itemView) {
            super(itemView);
            icon_choose = itemView.findViewById(R.id.icon_choose);
            rc_list = itemView.findViewById(R.id.rc_list);
            tv_money_del = itemView.findViewById(R.id.tv_money_del);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_add = itemView.findViewById(R.id.tv_add);
            tv_cut = itemView.findViewById(R.id.tv_cut);
            tv_num = itemView.findViewById(R.id.tv_num);
        }
    }
}
